<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    /**
    *   商品的添加页面
    */
    public function getAdd()
    {	
       
        //解析模板 分配数据
        return view('goods/add');

    }
    /**
    *	商品的添加方法
    */
     public function postInsert(Request $request)
    {
        
    	
        $this->validate($request, [
            'goodsname' => 'required',
            'goodspic' => 'required',
            'goodsprice' => 'required',
            'goodsnum' => 'required',
            'goodsdesc' => 'required',

        ],[
            'goodsname.required'=>'商品名不能为空',
            'goodspic.required'=>'商品图不能为空',
            'goodsprice.required' =>'商品价格不能为空',
            'goodsnum.required'=>'库存不能为空',
            'goodsdesc.required'=>'商品描述不能为空',
        ]);
      
      $res = $request->except('_token');
        
         // 检测是否有文件上传
      if($request-> hasFile('goodspic')){
        //随机名
        $name = md5(rand(10000,99999).time());

       // var_dump($name);
        //获取文件扩展名
        $suffix = $request->file('goodspic')->getClientOriginalExtension();
      	
      	$name = $name.'.'.$suffix;
      	//echo $name;
        $request->file('goodspic')->move('./imgs/',$name);
        
        $res['goodspic'] = '/imgs/'.$name;
		// dd($res);

      } 

       // 添加数据
       $pro = DB::table('goods')->insert($res);


        //判断是否成功并提示
        if($pro){

            return redirect('/admin/goods/index')->with('success','添加成功');
        } else {

            return back()->with('error','添加失败');
        }
        
    }

    /**
    *	商品列表
    */
    public function getIndex(Request $request)
    {
        //搜索的值


        //下拉的值  10 25 50 100

        //获取总数据
        $res = DB::table('goods')->
        where('goodsname','like','%'.$request->input('search').'%')->
        paginate($request->input('num',10));
       
        //解析模板  分配数据
        return view('goods/index',['row'=>$res,'request'=>$request->all()]);
    }

    // /**
    // *   商品的修改页面
    // */
    public function getEdit(Request $request)
    {
       $id = $request->input('id');

       $row = DB::table('goods')->where('id','=',$id)->first();

       return view('goods/edit',['row'=>$row]);
     }

     /**
    *   用户的修改方法
    */
    public function postUpdate(Request $request)
    {

        $this->validate($request, [
           
            'status' => 'required',
            
        ],[
                       
            'status.required'=>'商品状态不能为空',

        ]);
        
         $id = $request->input('id');
        
        $row = $request->except('_token','newgoodspic');
        
        
         // 检测是否有文件上传
      if($request->hasFile('newgoodspic')){
        //随机名
        $name = md5(rand(10000,99999).time());

      // var_dump($name);
        //获取文件扩展名
        $suffix = $request->file('newgoodspic')->getClientOriginalExtension();
      	
      	$name = $name.'.'.$suffix;
      	//echo $name;
        $request->file('newgoodspic')->move('./imgs/',$name);
        
        $row['goodspic'] = '/imgs/'.$name;
		// dd($row);
    }
  
        
        $res = DB::table('goods')->where('id',$id)->update($row);

        if($res) {

            return redirect('/admin/goods/index')->with('success','修改成功');
        } else {

            return back()->with('error','修改失败');
        }
    }

    /**
    *   用户的删除
    */
    public function getDelete(Request $request)
    {
        //获取id
        $id = $request->input('id');
        //执行删除
        $res = DB::table('goods')->where('id',$id)->delete();
        //判断
        if($res) {

            return redirect('/admin/goods/index')->with('success','删除成功');

        } else {
            return back()->with('error','删除失败');
        }
    }


}

  